package handler

import (
	"github.com/gin-gonic/gin"
	"net/http"
	"zk1/api/request"
	"zk1/internal/dao"
	"zk1/internal/model"
)

func InfoTestReq(c *gin.Context) {
	var req request.InfoTestReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	info := model.Test{Id: int64(req.Id)}

	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "展示成功",
		"data": info,
	})
}
func UpdateTest(c *gin.Context) {
	var req request.UpdateTestReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	update := model.Test{
		Id:   int64(req.Id),
		Name: req.Name,
		Test: req.Test,
	}

	if !dao.Update(&update) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "修改失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "修改成功",
		"data": update,
	})
}
func RemoveTest(c *gin.Context) {
	var req request.RemoveTestReq
	if err := c.ShouldBind(&req); err != nil {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "验证失败",
			"data": err.Error(),
		})
		return
	}
	if !dao.Delete(&model.Test{Id: int64(req.Id)}) {
		c.JSON(http.StatusOK, gin.H{
			"code": 10000,
			"msg":  "删除失败",
			"data": nil,
		})
		return
	}
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"msg":  "删除成功",
		"data": req,
	})
}
